/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.cancel.postcancel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.req.CancelOrderReq;
import com.lxk.oms.order.domain.vo.OmsOrderItemVo;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderItemService;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.ValidationUtils;

import java.util.List;

import static com.lxk.oms.flow.cancel.postcancel.FlowPostOrderCancelOrderNodeConst.POST_ORDER_CANCEL_FILL_ORDER_NODE;

/**
 * 填充对应的上下文
 *
 * @author heyi
 * 2024/9/3
 */
@AllArgsConstructor
@LiteflowComponent(POST_ORDER_CANCEL_FILL_ORDER_NODE)
public class CancelFillNode extends NodeComponent {
    private final IOmsOrderService omsOrderService;
    private final IOmsOrderItemService omsOrderItemService;
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);

    @Override
    public void beforeProcess() {
        CancelOrderReq request = this.getRequestData();
        ValidationUtils.validate(request);
        log.info("confirmOrderNode beforeProcess,{}", request);
        if (request.getPostOrderCancel() == null) {
            throw new ServiceException("参数异常");
        }
    }

    @Override
    public void process() throws Exception {
        CancelOrderReq request = this.getRequestData();
        PostCancelOrderContext contextBean = this.getContextBean(PostCancelOrderContext.class);
        Long userId = request.getUserId();
        String userType = request.getUserType();
        String orderNo = request.getOrderNo();
        OmsOrderVo omsOrderVo = omsOrderService.queryByOrderSn(orderNo, userId);
        if (ObjectUtil.isNull(omsOrderVo)) {
            log.error("取消 订单不存在:{},{}", orderNo, userId);
            throw new ServiceException("订单不存在");
        }
        List<OmsOrderItemVo> omsOrderItemVos = omsOrderItemService.queryByOrderIdList(omsOrderVo.getId());
        if (CollUtil.isEmpty(omsOrderItemVos)) {
            log.error("取消 订单详情不存在:{},{}", orderNo, userId);
            throw new ServiceException("订单详情不存在");
        }
        contextBean.setOutRefundNo(request.getOutRefundNo());
        contextBean.setItemVoList(omsOrderItemVos);
        contextBean.setOmsOrderVo(omsOrderVo);
        contextBean.setUserId(userId);
        contextBean.setOrderNo(orderNo);
        contextBean.setUserType(userType);
        contextBean.setCancelStatus(OrderEnums.OrderStatus.getByCode(request.getCancelCode()));
        contextBean.setSrcCancelStatusList(OrderEnums.OrderStatus.toListByCodeList(request.getSrcCancelStatusList()));
        contextBean.setCancelReason(request.getCancelReason());
        CancelOrderReq.PostOrderCancel postOrderCancel = request.getPostOrderCancel();
        contextBean.setRefundAmount(postOrderCancel.getRefundAmount());
        contextBean.setPostOrderItemList(postOrderCancel.getPostOrderItemList());
        ValidationUtils.validate(contextBean);
    }
}
